package com.ecelin;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;


import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class JSONFormat {
	private final JFrame jf = new JFrame("JSONFormat (将需要移除转译字符和格式化的json文件拖入下面空白处)");
	private final JTextArea textArea = new JTextArea(20, 60);
	Font font = new Font("微软雅黑", Font.PLAIN, 15);
	private final static String STRING1_0 = "\\\"";
	private final static String STRING1_1 = "\"";
	private final static String STRING2_0 = "\"{";
	private final static String STRING2_1 = "{";
	private final static String STRING3_0 = "}\"";
	private final static String STRING3_1 = "}";
	private final static String STRING4_0 = "\"[";
	private final static String STRING4_1 = "[";
	private final static String STRING5_0 = "]\"";
	private final static String STRING5_1 = "]";

	public static void main(String[] args) {
		new JSONFormat().init();
	}

	private void init() {
		textArea.setFont(font);
		new DropTarget(textArea, DnDConstants.ACTION_COPY_OR_MOVE, new DropTargetAdapter() {
			public void drop(DropTargetDropEvent dtde) {
				dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
				try {
					List<File> list = (List<File>) dtde.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
					textArea.setText("");
					for (File file : list) {
						handleFile(file);
					}
				} catch (UnsupportedFlavorException e) {
					textArea.append(e.getMessage() + "\n");
					dtde.rejectDrop();
				} catch (IOException e) {
					textArea.append(e.getMessage() + "\n");
				}
			}
		});
		jf.add(textArea);
		setCenterClosingPackVisible(jf);
	}

	private void handleFile(File file) {
		try {
			String replace = FileUtil.readString(file, StandardCharsets.UTF_8).trim();
			while (replace.contains(STRING1_0)) replace = replace.replace(STRING1_0, STRING1_1);
			replace = replace.replace(STRING2_0, STRING2_1);
			replace = replace.replace(STRING3_0, STRING3_1);
			replace = replace.replace(STRING4_0, STRING4_1);
			replace = replace.replace(STRING5_0, STRING5_1);
			int mediumBracketIndex = replace.indexOf("[");
			int bigBracketIndex = replace.indexOf("{");
			if (mediumBracketIndex == -1 && bigBracketIndex == -1) {
				throw new Exception(" 该文件不符合json格式规则,无法处理!");
			}
			JSON json;
			if (mediumBracketIndex == -1 || mediumBracketIndex > bigBracketIndex) {
				json = new JSONObject(replace);
			} else {
				json = new JSONArray(replace);
			}
			replace = json.toStringPretty();
			FileUtil.writeString(replace, file, StandardCharsets.UTF_8);
			textArea.append(file.getName() + " 处理成功!\n");
		} catch (Exception e) {
			textArea.append(file.getName() + " 处理失败!" + e.getMessage() + "\n");
		}
	}

	private void setCenterClosingPackVisible(Window window) {
		window.pack();
		int x = (int) ((Toolkit.getDefaultToolkit().getScreenSize().getWidth() - window.getWidth()) / 2);
		int y = (int) ((Toolkit.getDefaultToolkit().getScreenSize().getHeight() - window.getHeight()) / 2);
		window.setLocation(x, y);
		window.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				String string = e.getComponent().toString();
				if (string.contains(".Dialog")) {
					window.setVisible(false);
				} else if (string.contains(".Frame") || string.contains(".JFrame")) {
					System.exit(0);
				}
			}
		});
		window.setVisible(true);
	}
}
